package exercises.leetcode;

import exercises.newcoder.util.TreeNode;

/**
 * <a href="https://leetcode-cn.com/problems/symmetric-tree/">
 * 101. 对称二叉树</a>
 *
 * <p><span color="#87CEEB">描述:</span>
 * 给定一个二叉树，检查它是否是镜像对称的。
 *
 * @author or2
 * @date 2021年10月13日 时间: 19:35
 */
public class No101 {
    public boolean isSymmetric(TreeNode root) {
        if (root == null)
            return false;

        return sysmetry(root.left, root.right);
    }

    private boolean sysmetry(TreeNode left, TreeNode right) {
        if (left == null && right == null)
            return true;

        if (left == null || right == null || left.val != right.val)
            return false;

        return sysmetry(left.left, right.right) && sysmetry(left.right, right.left);
    }
}
